<template>
  <div class="body">
    <header class="header">
      <div class="title">
        <img
          src="../../../assets/images/01_11.png"
          alt=""
          class="img_zuo"
          @click="PrevGo"
        />
        <div>扫一扫</div>
        <!-- <img src="../../../assets/images/tj.png" alt="" class="img_jia" @click="add"> -->
        <span class="xiangce" @click="scanPicture">相册</span>
      </div>
    </header>
    <div id="code" class="code"></div>
  </div>
</template>

<script>
export default {
  name: "xcpc_scan",
  data() {
    return {
      scan: null,
    };
  },
  methods: {
    //创建扫描控件
    startRecognize() {
      let that = this;
      if (!window.plus) return;
      // 创建条码扫描识别控件
      console.log(123);
      try {
        var wH = window.screen.height;
        var wW = window.screen.width;
        console.log(wH);
        console.log(wW);
        $("#code").height(wH - $(".header").height());
        $("#code").width(wW);
        var filters;
        //自定义的扫描控件样式
        var styles = {
          frameColor: "#4367fd",
          scanbarColor: "#4367fd",
        };
        //扫描控件构造
        that.scan = new plus.barcode.Barcode("code", filters, styles);
        that.scan.start();
        console.log(1111);
        that.scan.onmarked = onmarked;
        that.scan.onerror = function (e) {
          console.log("扫码出错:\n" + e);
          alert("扫码出错，请稍后再试！");
        };
        function onmarked(type, result, file) {
          switch (type) {
            case plus.barcode.QR:
              type = "QR";
              break;
            case plus.barcode.EAN13:
              type = "EAN13";
              break;
            case plus.barcode.EAN8:
              type = "EAN8";
              break;
            default:
              type = "其它" + type;
              break;
          }
          result = result.replace(/\n/g, "");
          // alert(JSON.stringify(result))
          if (result.split("+").length == 5) {
            let data = { id: result.split("+")[3], type: result.split("+")[4] };
            sessionStorage.setItem("scan_canshu_data", JSON.stringify(data));
            that.scan.cancel(); //关闭扫描
            that.scan.close(); //关闭条码识别控件
            let a = Number(result.split("+")[4]);
            // that.bc(result.split("+")[3],result.split("+")[4],that.userId)
            if (a === 1) {
              that.$router.push("/inspect_detail_building");
            } else if (a === 2) {
              that.$router.push("/inspect_detail");
            } else if (a === 3) {
              that.$router.push("/inspect_detail_zgf");
            } else if (a === 4) {
              that.$router.push("/inspect_detail_qy");
            }
            return false;
          } else if (result.split("+").length == 4) {
            let data = { id: result.split("+")[2], type: result.split("+")[3] };
            sessionStorage.setItem("scan_canshu_data", JSON.stringify(data));
            that.scan.cancel(); //关闭扫描
            that.scan.close(); //关闭条码识别控件
            let a = Number(result.split("+")[3]);
            // that.bc(result.split("+")[3],result.split("+")[4],that.userId)
            if (a === 1) {
              that.$router.push("/inspect_detail_building");
            } else if (a === 2) {
              that.$router.push("/inspect_detail");
            } else if (a === 3) {
              that.$router.push("/inspect_detail_zgf");
            } else if (a === 4) {
              that.$router.push("/inspect_detail_qy");
            }
            return false;
          }

          that.scan.start();
          // let uuId = result.split("=");
          // console.log(JSON.stringify(uuId));
          // console.log(result);
          // scan.cancel(); //关闭扫描
          // scan.close(); //关闭条码识别控件
        }
      } catch (e) {
        console.log("扫码出错:\n" + e);
        alert("扫码出错，请稍后再试！");
      }
    },
    //开始扫描
    startScan() {
      if (!window.plus) return;
      this.startRecognize(); //创建控件
    },

    PrevGo() {
      this.scan.cancel(); //关闭扫描
      this.scan.close(); //关闭条码识别控件
      this.$router.back();
    },
    scanPicture() {
      //相册
      let that = this;
      plus.gallery.pick(function (path) {
        plus.barcode.scan(path, onmarked, function (error) {
          console.log(error);
          plus.nativeUI.alert("无法识别此图片！");
        });
      });

      function onmarked(type, result, file) {
        switch (type) {
          case plus.barcode.QR:
            type = "QR";
            break;
          case plus.barcode.EAN13:
            type = "EAN13";
            break;
          case plus.barcode.EAN8:
            type = "EAN8";
            break;
          default:
            type = "其它" + type;
            break;
        }
        result = result.replace(/\n/g, "");
        if (result.split("+").length == 5) {
          let data = { id: result.split("+")[3], type: result.split("+")[4] };
          // alert(JSON.stringify(data))
          sessionStorage.setItem("scan_canshu_data", JSON.stringify(data));
          that.scan.cancel(); //关闭扫描
          that.scan.close(); //关闭条码识别控件
          let a = Number(result.split("+")[4]);
          if (a === 1) {
            that.$router.push("/inspect_detail_building");
          } else if (a === 2) {
            that.$router.push("/inspect_detail");
          } else if (a === 3) {
            that.$router.push("/inspect_detail_zgf");
          } else if (a === 4) {
            that.$router.push("/inspect_detail_qy");
          }
          return false;
        } else if (result.split("+").length == 4) {
          let data = { id: result.split("+")[2], type: result.split("+")[3] };
          sessionStorage.setItem("scan_canshu_data", JSON.stringify(data));
          that.scan.cancel(); //关闭扫描
          that.scan.close(); //关闭条码识别控件
          let a = Number(result.split("+")[3]);
          // that.bc(result.split("+")[3],result.split("+")[4],that.userId)
          if (a === 1) {
            that.$router.push("/inspect_detail_building");
          } else if (a === 2) {
            that.$router.push("/inspect_detail");
          } else if (a === 3) {
            that.$router.push("/inspect_detail_zgf");
          } else if (a === 4) {
            that.$router.push("/inspect_detail_qy");
          }
          return false;
        }

        that.scan.start();
        // let uuId = result.split("=");
        // console.log(JSON.stringify(uuId));
        // console.log(result);
        // scan.cancel(); //关闭扫描
        // scan.close(); //关闭条码识别控件
      }
    },
    plusReady(){
      this.scan.cancel(); //关闭扫描
      this.scan.close(); //关闭条码识别控件
    },
  },
  mounted() {
    var _this = this;
    // this.startRecognize();
    this.startScan();
    document.body.style.overflowY = "auto";
    // if (window.history && window.history.pushState) {
    //   history.pushState(null, null, document.URL);
    //   window.addEventListener("popstate", this.plusReady, false); //false阻止默认事件
    // }
    //物理返回
    // window.addEventListener("popstate", function(){
    //   _this.PrevGo();
    // }, false);
  },
  destroyed() {
    this.plusReady()
    // window.removeEventListener("popstate", this.plusReady, false); //false阻止默认事件
  },
  created() {
    this.userId = localStorage.getItem("userId"); //获取访问id
    // this.startRecognize();
    // this.startScan();
  },
};
</script>

<style scoped>
#code {
  position: absolute;
  margin-top: 1.8519rem;
  width: 100%;
  height: 100%;
  background: #000000;
}

.header {
  height: 1.8519rem;
  background: #007aff;
  padding-top: 0.8333rem;
  box-sizing: border-box;
  position: fixed;
  width: 100%;
  top: 0;
  z-index: 20;
  font-size: 0.388888rem;
}

.title {
  padding: 0 0.3704rem;
  display: flex;
  align-items: center;
  color: #fff;
}

.title > div {
  width: 100%;
  font-size: 0.4444rem;
  text-align: center;
}

.img_zuo {
  width: 0.2593rem;
  height: 0.463rem;
}

.img_jia {
  width: 0.463rem;
  height: 0.463rem;
  position: absolute;
  right: 0.3704rem;
}

.xiangce {
  font-size: 0.3889rem;
  position: absolute;
  right: 0.3704rem;
}
</style>
